import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'


Vue.use(VueRouter)



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/**
 * 引入文件中的某一个对象
 */
import { get_token } from '@/utils/auth'

/**
 * 进度条
 */
import nProgress from 'nprogress'

import { check_login } from '@/api/login'

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || "在线学习平台";
  nProgress.start();
  if (!to.meta.not_check) {
    if (!get_token()) {
      next('login');
      return;
    }

    check_login({}).then((res) => {
      if (res.code === 0) {
        sessionStorage.setItem("user", JSON.stringify(res.data));
      }
    });
  }

  next();
});

router.afterEach(() => {
  nProgress.done();
});


//暴露整个文件
export default router
